﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using SPK;
using SPK.System;

public partial class _Default : System.Web.UI.Page 
{
    /// <summary>
    /// 每一页显示的记录数量，默认值为9
    /// </summary>
    private int PageCount = 9;
    /// <summary>
    /// 当前页码
    /// </summary>
    private int CurrentPageIndex = -1;
    /// <summary>
    /// 总页码数量
    /// </summary>
    private int TotalPageIndex = 0;

   
    protected void Page_Load(object sender, EventArgs e)
    {
        ///获取总页码数量
        if (ViewState[WebSystem.TOTALPAGEINDEX] != null)
        {
            TotalPageIndex = Int32.Parse(ViewState[WebSystem.TOTALPAGEINDEX].ToString());
        }
        ///获取当前页码
        if (ViewState[WebSystem.CURRENTPAGEINDEX] != null)
        {
            CurrentPageIndex = Int32.Parse(ViewState[WebSystem.CURRENTPAGEINDEX].ToString());
        }
        ///初始化页面的数据，并设置为第一页。

        if (!Page.IsPostBack)
        {
            PagingDataInit();
            BindCurrentPageData();
        }
    }

    private void PagingDataInit()
    {   ///获取数据
        ///
        BookMange book = new BookMange();
        DataSet ds=book.GetAllBook();
        if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
            return;
        if (PageCount == 0) return;
        int num = ds.Tables[0].Rows.Count;
        TotalPageIndex = ds.Tables[0].Rows.Count / PageCount;
        if (TotalPageIndex * PageCount < ds.Tables[0].Rows.Count)
            TotalPageIndex++;
        ViewState[WebSystem.TOTALPAGEINDEX] = TotalPageIndex;
        if (TotalPageIndex > 0)
        {
            CurrentPageIndex = 0;
            ViewState[WebSystem.CURRENTPAGEINDEX] = CurrentPageIndex;
        }
    }

    private void BindCurrentPageData()
    {
        if (PageCount <= 0 || CurrentPageIndex < 0) return;
        ///获取数据
        BookMange book = new BookMange();
        DataSet dsCurrent = book.GetAllBook();
        ///显示数据
        PagedDataSource psd = new PagedDataSource();
        psd.DataSource = dsCurrent.Tables[0].DefaultView;
        psd.AllowPaging = true;
        psd.PageSize = PageCount;
        psd.CurrentPageIndex = CurrentPageIndex;
        DefaultShow.DataSource = psd;
        DefaultShow.DataBind();
       
       
        ///控制分页按钮的可用性
         SetPageButtonEnable();
         ShowCurrentIndex();
    }

    protected void Page_Command(object sender, CommandEventArgs e)
    {
        string commandName = e.CommandName.ToLower();
        if (string.IsNullOrEmpty(commandName) == true) return;
        ///判断总页码数量是否合法
        if (TotalPageIndex <= 0) return;

        switch (commandName)
        {
            case "first":   ///首页
                {
                    CurrentPageIndex = 0;
                    break;
                }
            case "prev":   ///上一页
                {
                    CurrentPageIndex = Math.Max(0, CurrentPageIndex - 1);
                    break;
                }
            case "next":   ///下一页
                {
                    CurrentPageIndex = Math.Min(TotalPageIndex - 1, CurrentPageIndex + 1);
                    break;
                }
            case "last":   ///末页
                {
                    CurrentPageIndex = TotalPageIndex - 1;
                    break;
                }
            case "move":   ///直接跳转到指定的页码
                {
                    int page = Int32.Parse(tbMove.Text.Trim());
                    if (page > 0 && page <= PageCount)
                    {
                        CurrentPageIndex = page - 1;
                    }
                    break;
                }
            default: break;
        }
        ViewState[WebSystem.CURRENTPAGEINDEX] = CurrentPageIndex;
        ///重新绑定控件的数据
        BindCurrentPageData();
    }
    private void SetPageButtonEnable()
	{
		///判断总页码数量是否合法
		if(TotalPageIndex <= 0) return;

		///如果页数量小于1，则所有分页按钮不可用
		if(TotalPageIndex <= 1)
		{
			ibtFirst.Enabled = ibtPrev.Enabled = ibtNext.Enabled = ibtLast.Enabled = ibtMove.Enabled = false;
			return;
		}
		///如果是第一页，【上一页】按钮不可用
		ibtPrev.Enabled = CurrentPageIndex == 0 ? false : true;
		///如果是最后一页，【下一页】按钮不可用
		ibtNext.Enabled = CurrentPageIndex == TotalPageIndex - 1 ? false : true;
		tbMove.Enabled = true;
	}

	/// <summary>
	/// 设置当前页码和所有页码
	/// </summary>
	private void ShowCurrentIndex()
	{
		///判断总页码数量是否合法
		if(TotalPageIndex <= 0) return;
		///显示页码信息
		lbCurrentIndex.Text = "当前第" + (CurrentPageIndex + 1).ToString()
			+ "页，共" + TotalPageIndex.ToString() + "页";
	}


    protected void tbMove_TextChanged(object sender, EventArgs e)
    {
        int page = Int32.Parse(tbMove.Text.Trim());
        if (page > 0 && page <= PageCount)
        {
            CurrentPageIndex = page - 1;
        }

        ViewState[WebSystem.CURRENTPAGEINDEX] = CurrentPageIndex;
        BindCurrentPageData();
    }


}
